<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <div id="app">
        <h2>组件A：向总线上报事件</h2>
        <my-component-a :counter="total"></my-component-a>
        <h2>组件B： 通过总线监听相关事件</h2>
        <my-component-b></my-component-b>
    </div>
    <template id="temp">
        <div>
            <p>组件A</p><hr>
            <button @click="doClick">{{counter}}</button>
        </div>
    </template>
    <template id="templ">
        <div>
            <p>组件B</p>
            <hr>计数器：{{counter}}<hr>
        </div>
    </template>
    <script type="text/javaScript" charset="UTF-8" src="/JS文件/vue.js"></script>
    <script>
        var bus = new Vue();
        Vue.component("my-component-a",{
            template:"#temp",
            data(){
                return {
                    counter:0
                }
            },
            methods:{
                doClick(){
                    this.counter++;
                    bus.$emit("btn-click",this.counter)
                }
            }
        })
        Vue.component("my-component-b",{
            template:"#templ",
            data(){
                return {
                    counter:0
                }
            },
            methods:{
                foo(value){
                    this.counter = value;
                }
            },
            created(){
                bus.$on("btn-click",this.foo);
            }
        })
        var app = new Vue({
            el:"#app",
            data:{
                total:0
            },
            methods:{
                doChildClick(){
                    this.total+=1
                }
            }
        })
    </script>
</body>
</html>